package com.jiedeng.controller;

import com.jiedeng.dao.ICorpPostRepository;
import com.jiedeng.dao.ICorpRepository;
import com.jiedeng.dao.IPersonRepository;
import com.jiedeng.model.Corp;
import com.jiedeng.model.CorpPost;
import com.jiedeng.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 覃远龙
 * @ClassName: CorpController
 * @Description:
 * @date 2017/6/27 15:59
 */
@RestController
@RequestMapping("/corp")
public class CorpController {

    @Autowired
    ICorpRepository corpRepository;

    @Autowired
    ICorpPostRepository corpPostRepository;

    @Autowired
    IPersonRepository personRepository;

    @RequestMapping(value = "/all_person", method = RequestMethod.GET)
    public List<Person> findAllPerson(){
        return personRepository.findAll();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Corp> findAllCorp(){
        List<Corp> corps = corpRepository.findAll();
        Corp globalCorp = new Corp();
        globalCorp.setId(-1);
        globalCorp.setCaption("全局");
        corps.add(globalCorp);

        return corps;
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public List<Corp> searchCorp(String corpStr){
        return corpRepository.findByCaptionContaining(corpStr);
    }

    @RequestMapping(value = "/searchByOBS", method = RequestMethod.GET)
    public List<Corp> searchCorpByOBS(long obsId){
        return corpRepository.findByObsId(obsId);
    }

    @RequestMapping(value = "/allCorpPosts", method = RequestMethod.GET)
    public List<CorpPost> findAllCorpPosts(){
        return corpPostRepository.findAll();
    }

    @RequestMapping(value = "/posts", method = RequestMethod.GET)
    public List<CorpPost> findCorpPosts(int corpId){
        return corpPostRepository.findByCorpId(corpId);
    }

    @RequestMapping(value = "/post/save", method = RequestMethod.POST)
    public CorpPost savePost(@RequestBody CorpPost formPost){
        return corpPostRepository.save(formPost);
    }

    @RequestMapping(value = "/post/delete", method = RequestMethod.POST)
    public void deletePost(long id){
        corpPostRepository.deleteById(id);
    }

    @RequestMapping(value = "/persons", method = RequestMethod.GET)
    public List<Person> corpPersonList(long corpId){
        return personRepository.findByCorpId(corpId);
    }

    @RequestMapping(value = "/person", method = RequestMethod.GET)
    public Person getPerson(long persionId){
        return personRepository.getOne(persionId);
    }

    @RequestMapping(value = "/post/persons", method = RequestMethod.GET)
    public List<Person> postPersonList(int postId){
        return personRepository.findByPostId(postId);
    }

    @RequestMapping(value = "/person/save", method = RequestMethod.POST)
    public Person savePerson(@RequestBody Person person){
        if(person.getId() == 0){
            person.setStartTime(new Date());
            person.setAddTime(new Date());
            if(person.getEndTime() == null){
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.YEAR, 100);
                person.setEndTime(cal.getTime());
            }
        }

        return personRepository.save(person);
    }

    @RequestMapping(value = "/person/delete", method = RequestMethod.POST)
    public void deletePerson(long id){
        personRepository.deleteById(id);
    }

}
